﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
namespace ChangDiTools {
    public static class PolylineTools {
        /// <summary>
        /// 通过三维点集合创建多段线
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline, Point3dCollection pts)
        {
            for (int i = 0; i < pts.Count; i++)
            {
                pline.AddVertexAt(i, new Point2d(pts[i].X, pts[i].Y), 0, 0, 0);
            }
        }
        /// <summary>
        /// 通过二维点集合创建多段线
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline, Point2dCollection pts)
        {
            for (int i = 0; i < pts.Count; i++)
            {
                pline.AddVertexAt(i, pts[i], 0, 0, 0);
            }
        }
        /// <summary>
        /// 通过多个点创建多段线
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pts">多段线的顶点</param>
        public static void CreatePolyline(this Polyline pline, params Point2d[] pts)
        {
            pline.CreatePolyline(new Point2dCollection(pts));
        }
        /// <summary>
        /// 创建矩形
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="pt1">矩形的角点</param>
        /// <param name="pt2">矩形的角点</param>
        public static void CreateRectangle(this Polyline pline, Point2d pt1, Point2d pt2)
        {
            double minX = Math.Min(pt1.X, pt2.X);
            double maxX = Math.Max(pt1.X, pt2.X);
            double minY = Math.Min(pt1.Y, pt2.Y);
            double maxY = Math.Max(pt1.Y, pt2.Y);
            Point2dCollection pts=new Point2dCollection();
            pts.Add(new Point2d(minX, minY));
            pts.Add(new Point2d(minX, maxY));
            pts.Add(new Point2d(maxX, minY));
            pts.Add(new Point2d(maxX, maxY));
            pline.CreatePolyline(pts);
            pline.Closed = true;
        }
        public static void CreateRectangle(this Polyline pline, Point3d pt1, Point3d pt2)
        {
            Point2d p1=new Point2d(pt1.X,pt1.Y);
            Point2d p2=new Point2d(pt2.X,pt2.Y);
            pline.CreateRectangle(p1,p2);
        }
        /// <summary>
        /// 创建多边形
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="centerPoint">多边形中心点</param>
        /// <param name="number">边数</param>
        /// <param name="radius">外接圆半径</param>
        public static void CreatePolygon(this Polyline pline, Point2d centerpoint, int number, double radius)
        {
            Point2dCollection pts=new Point2dCollection(number);
            double angle = 2 * Math.PI / number;
            for (int i = 0; i < number; i++)
            {
                Point2d pt=new Point2d(centerpoint.X+radius*Math.Cos(i*angle),centerpoint.Y+radius*Math.Sin(i*angle));
                pts.Add(pt);
            }
            pline.CreatePolyline(pts);
            pline.Closed = true;
        }
        public static void CreatePolygon(this Polyline pline, Point3d centerpoint, int number, double radius) {
            Point2d p1 = new Point2d(centerpoint.X, centerpoint.Y);
            pline.CreatePolygon(p1, number, radius);
        }
        /// <summary>
        /// 创建多段线形式的圆
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="centerPoint">圆心</param>
        /// <param name="radius">半径</param>
        public static void CreatePolyCircle(this Polyline pline, Point2d centerpoint, double radius)
        {
            Point2d pt1=new Point2d(centerpoint.X+radius,centerpoint.Y);
            Point2d pt2=new Point2d(centerpoint.X-radius,centerpoint.Y);
            pline.AddVertexAt(0,pt1,1,0,0);
            pline.AddVertexAt(1,pt2,1,0,0);
            pline.AddVertexAt(2,pt1,1,0,0);
            pline.Closed = true;
        }
        public static void CreatePolyCircle(this Polyline pline, Point3d centerpoint, double radius) {
            Point2d p1 = new Point2d(centerpoint.X, centerpoint.Y);
            pline.CreatePolyCircle(p1,radius);
        }
        /// <summary>
        /// 创建多段线形式的圆弧
        /// </summary>
        /// <param name="pline">多段线对象</param>
        /// <param name="centerPoint">圆弧的圆心</param>
        /// <param name="radius">圆弧的半径</param>
        /// <param name="startAngle">起始角度</param>
        /// <param name="endAngle">终止角度</param>
        public static void CreatePolyArc(this Polyline pline, Point2d centerPoint, double radius, double startAngle,
            double endAngle)
        {
            Point2d pt1=new Point2d(centerPoint.X+radius*Math.Cos(startAngle),
                centerPoint.Y+radius*Math.Sin(startAngle));
            Point2d pt2=new Point2d(centerPoint.X+radius*Math.Cos(endAngle),
                centerPoint.Y+radius*Math.Sin(endAngle));
            pline.AddVertexAt(0,pt1,Math.Tan((endAngle - startAngle) / 4),0,0);//角度1/4的正切值
            pline.AddVertexAt(1,pt2,0,0,0);
        }
        public static void CreatePolyArc(this Polyline pline, Point3d centerPoint, double radius, double startAngle,
            double endAngle) {
            Point2d p1 = new Point2d(centerPoint.X, centerPoint.Y);
            pline.CreatePolyArc(p1, radius, startAngle, endAngle);
        }
    }
}
